<template>
    <div class="login">
        <div class="header">
            <div class="logo-box">
                <img class="logo" :src="logo" />
                <div class="app-name">
                    <div class="en">FD.EAP V2.0</div>
                    <div class="cn">福大自动化EAP</div>
                </div>
            </div>
            <div class="content">
                <div class="version">
                    <span>{{ version }}</span>
                </div>
            </div>
        </div>
        <div class="content">
            <el-form :model="form" label-width="auto" class="form">
                <el-form-item label="工 号">
                    <el-input v-model="form.name" placeholder="请输入工号" />
                </el-form-item>
                <el-form-item label="密 码">
                    <el-input type="password" placeholder="请输入密码" v-model="form.password" />
                </el-form-item>
                <el-form-item>
                    <div class="button-section">
                        <el-button type="primary" @click="login(true)" :loading="logining">登录EAP</el-button>
                        <el-button type="success" @click="login(false)" :loading="logining">登录邮件</el-button>
                    </div>
                </el-form-item>
            </el-form>
            <!-- <button @click="login">登录</button> -->
        </div>
        <div class="footer">
            <div class="qrcode">
                <img :src="qrcode" alt="">
            </div>
            <div class="right">
                <div class="service">
                    <div class="item">
                        <img :src="tel" alt=""><span>客服电话:0591-38726695</span>
                    </div>
                    <div class="item">
                        <img :src="qq" alt=""><span>客服QQ:3105705872 2475225397</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { ipc, isEE } from "@/utils/ipcRenderer";

import { ref, onMounted, reactive } from "vue";

import { useUserStore } from "@/store/userStore";
import { ipcApiRoute } from "@/api";
import { useRouter } from 'vue-router'
import logo from '@/assets/images/fd-logo.png'
import tel from '@/assets/images/tel.png'
import qq from '@/assets/images/qq.png'
import qrcode from '@/assets/images/yx-qrcode.png'
import { ElMessage } from 'element-plus'
const userStore = useUserStore()
const logining = ref(false)
const version = ref("")
const form = reactive({
    name: '',
    password: ''
})
const router = useRouter()
function validate() {
    if (!form.name) {
        ElMessage.error("请输入工号");
        return false
    }
    if (!form.password) {
        ElMessage.error("请输入密码");
        return false
    }
    return true;
}

function login(isEap) {
    if (!validate())
        return;
    logining.value = true;
    setTimeout(async () => {
        const result = await userStore.login(form.name, form.password);
        logining.value = false;
        if (!result) {
            ElMessage.error("登录失败,用户名或密码错误");
            return;
        }

        router.push({ path: "/" })
        if (isEE)
            ipc.send(ipcApiRoute.maximizeWindow)
    }, 1000);

}
function getVersion() {
    if (!isEE) {
        return;
    }
    ipc.invoke(ipcApiRoute.app.getVersion).then(res => {
        version.value = `版本:${res}`
    })

}
onMounted(() => {
    getVersion()
})
</script>
<style scoped lang="less">
.login {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;

    .header {
        width: 100%;
        height: 80px;
        background: url('@/assets/images/login-bg.jpg');
        background-size: 100% 100%;
        /* 宽度拉伸至100%，高度保持原始比例 */
        background-position: top;
        /* 从顶部开始 */
        background-repeat: no-repeat;
        padding-top: 20px;
        padding-left: 30px;
        box-sizing: border-box;


        /* 不重复 */
        .logo-box {
            display: flex;
            float: left;

            .logo {
                height: 100%;
            }

            .app-name {
                margin-left: 10px;
                display: flex;
                flex-direction: column;
                justify-content: center;
                text-align: left;

                .en {
                    font-size: 15px;
                    font-weight: 600;
                    color: #364873;
                }

                .cn {
                    font-weight: 600;
                    font-size: 8px;
                }

            }
        }

        .content {
            float: right;
            margin-right: 10px;
            display: flex;
            justify-content: right;

            .version {
                font-size: 12px;
                font-weight: 100;
                color: #fff;
            }
        }
    }

    .content {
        padding: 0 10px;

        .form {
            margin: 0 auto;
            margin-top: 30px;
            padding: 0 150px;
            max-width: 600px;
            min-width: 300px;

            .button-section {
                width: 100%;
                text-align: center;

                &>button:last-child {
                    margin-left: 50px;
                }
            }
        }

        flex: 1;
    }

    .footer {


        box-sizing: border-box;

        padding-left: 60px;
        margin: 0 auto;

        width: 100%;
        height: 120px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .right {
            flex: 1;
            padding: 0 10px;

            &::before {
                display: block;
                content: '';
                height: 1px;
                width: 100%;
                background-color: #328BD0;
            }

            .service {
                margin-top: 8px;
                display: flex;
                justify-content: right;
                align-items: center;
                gap: 20px;

                .item {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                }
            }
        }

    }
}
</style>